package com.example.keshe0337.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.keshe0337.R;
import com.example.keshe0337.adapter.RentAdapter;
import com.example.keshe0337.biz.RecordService;
import com.example.keshe0337.biz.RentService;
import com.example.keshe0337.entity.ParkingSpace;
import com.example.keshe0337.entity.Rent;

import java.util.List;

public class MyParkingFragment extends Fragment {
    ListView listView;
    Context context;
    RentService rentService;
    RecordService recordService;
    RentAdapter rentAdapter;
    Long UID;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view0 = inflater.inflate(R.layout.fragment_my_parking, container, false);
        listView = view0.findViewById(R.id.listView);  // 用 view 来查找 ListView
        context = getContext();
        if (context == null) {
            Log.d("TEXT","MyParkingFragment的context为空");
            return view0;
        }
        rentService = new RentService(context);
        recordService = new RecordService(context);
        SharedPreferences prefs = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        UID = prefs.getLong("UID", 1);
        List<Rent> allRents = rentService.getRentsByUid(UID);
        for (Rent p :allRents) {
            Log.d("TEXT_RENT",p.getSpaceNum());
        }

        rentAdapter = new RentAdapter(context,allRents);
        listView.setAdapter(rentAdapter);

        Button btn = view0.findViewById(R.id.searchButton);
        View text = view0.findViewById(R.id.searchEditText);
        btn.setOnClickListener(v -> {
            String searchText = "";
            if (text instanceof EditText) {
                EditText editTextPlate = (EditText) text;
                searchText = editTextPlate.getText().toString().trim();
            }
            if (searchText.isEmpty()) {
                rentAdapter.update(rentService.getAllRents());
            } else {
                List<Rent> rents = rentService.getRentsByUidByNum(1L,searchText);
                rentAdapter.update(rents);
            }
        });
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Rent rent = rentAdapter.getItem(position);
            if (rent != null) {
                boolean um = prefs.getBoolean("manager",false);
                if (um) {

                } else {
                    new AlertDialog.Builder(context)
                            .setTitle("确认归还")
                            .setMessage("是否确认归还该车位？\n停车位：" + rent.getSpaceNum())
                            .setPositiveButton("确认", (dialog, which) -> {
                                int i = recordService.endRent(rent.getId());
                                rentAdapter.update(rentService.getRentsByUid(UID));
                                if (i == 0) {
                                    Toast.makeText(context, "归还成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(context, "归还失败，请重试", Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNegativeButton("取消", (dialog, which) -> {
                            })// 用户点击取消则关闭会话
                            .show();
                }
            }
        });
        return view0;
    }
}
